package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.carptrip.CarpTripConfig;
import com.yanqu.road.entity.activity.carptrip.config.*;
import com.yanqu.road.entity.activity.carptrip.data.CarpTripPvPReportData;
import com.yanqu.road.entity.activity.carptrip.data.UserCarpTripData;
import com.yanqu.road.entity.activity.carptrip.enums.eCarpTripEventType;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.CarpTripProto;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CarpTripActivityPb {

    public static CarpTripProto.CarpTripUserDataTemp.Builder buildUserData(UserCarpTripData userData, CarpTripRoleInfo roleInfo) {
        CarpTripProto.CarpTripUserDataTemp.Builder builder = CarpTripProto.CarpTripUserDataTemp.newBuilder();
        builder.setLv(roleInfo.getLevel());
        builder.setExperience(userData.getExp().toString());
        builder.setPower(userData.getPower());
        builder.setLastRecoverPowerTime(userData.getLastRecoverPowerTime());
        builder.setScore(userData.getScore());
        builder.setAbility(userData.getAbility().toString());
        builder.setMultiple(roleInfo.getAddEffect());
        builder.setCellNum(userData.getCellNum());
        builder.setGameData(userData.getGameData());
        builder.setTopCellNum(userData.getTopCellNum());
        builder.addAllTopCellList(userData.getTopCellList());
        builder.setDrawUpLv(userData.getDrawUpLv());
        builder.setDrawYuZi(userData.getDrawExp());
        builder.setLaunchTimes(userData.getLaunchTimes());
        //builder.addAllDragonUpId(userData.getDragonIdList());
        CarpTripProto.CarpTripEventDataTemp.Builder event = CarpTripProto.CarpTripEventDataTemp.newBuilder();
        event.setPassId(userData.getEvent().getPassId());
        event.setCell(userData.getEvent().getCell());
        event.setColIndex(userData.getEvent().getColIndex());
        for (Map.Entry<Integer, Boolean> entry : userData.getDragonIdList().entrySet()) {
            CarpTripProto.CarpTripDragonUpRewardTemp.Builder dragonUpBuilder = CarpTripProto.CarpTripDragonUpRewardTemp.newBuilder();
            dragonUpBuilder.setId(entry.getKey());
            dragonUpBuilder.setWin(entry.getValue());
            builder.addDragonUp(dragonUpBuilder);
        }
        builder.setEvent(event);
        for (Map.Entry<Integer, Integer> entry : userData.getDrawCountMap().entrySet()) {
            CarpTripProto.CarpTripKeyValuePairsTemp.Builder drawCountBuilder = CarpTripProto.CarpTripKeyValuePairsTemp.newBuilder();
            drawCountBuilder.setId(entry.getKey());
            drawCountBuilder.setVal(entry.getValue());
            builder.addDrawGet(drawCountBuilder);
        }
        builder.setClientSave(userData.getClientSave());
        return builder;
    }


    public static CarpTripProto.CarpTripRoleConfigTemp.Builder buildRoleConfig(CarpTripRoleInfo roleInfo) {
        CarpTripProto.CarpTripRoleConfigTemp.Builder builder = CarpTripProto.CarpTripRoleConfigTemp.newBuilder();
        builder.setId(roleInfo.getId());
        builder.setLevel(roleInfo.getLevel());
        builder.setExp(roleInfo.getExp().toString());
        builder.setAddEffect(roleInfo.getAddEffect());
        builder.setPic(roleInfo.getPic());
        builder.setArmPic(roleInfo.getArmsPic());
        return builder;
    }

    public static List<CarpTripProto.CarpTripPassCellTemp.Builder> buildLevelConfig(CarpTripLevelInfo levelInfo, CarpTripConfig config, List<Integer> handledList) {
        List<CarpTripProto.CarpTripPassCellTemp.Builder> list = new ArrayList<>();
        for (int i = 0; i < levelInfo.getCellList().size(); i++) {
            CarpTripCell towerCell = levelInfo.getCellList().get(i);
            CarpTripProto.CarpTripPassCellTemp.Builder builder = CarpTripProto.CarpTripPassCellTemp.newBuilder();
            builder.setPassId(levelInfo.getId());
            builder.setCellId(i);
            builder.setCellType(levelInfo.getCellTypeList().get(i));
            builder.setTemplateId(levelInfo.getTemplateId());
            builder.setCellNum(towerCell.getCellNum());
            for (CarpTripCellEvent cellEvent : towerCell.getEventList()) {
                CarpTripProto.CarpTripCellEventTemp.Builder eventBuilder = CarpTripProto.CarpTripCellEventTemp.newBuilder();
                eventBuilder.setEventId(cellEvent.getEventType());
                eventBuilder.setEventType(cellEvent.getEventType());
                eventBuilder.setEventVal(cellEvent.getEventVal());
                eventBuilder.setPic(cellEvent.getPic());
                eventBuilder.setRawVal(cellEvent.getRawVal());
                if(cellEvent.getEventType() == eCarpTripEventType.Weapons.getType() && handledList.contains(towerCell.getCellNum())){
                    eventBuilder.setHadHandle(true);
                }
                builder.addEvent(eventBuilder);
            }
            builder.setBossID(levelInfo.getBossId());
            list.add(builder);
        }
        return list;
    }

    public static void buildOtherConfig(CarpTripConfig config, CarpTripProto.CarpTripConfigSyncMsg.Builder configSync) {

        for (CarpTripRoleInfo roleInfo : config.getRoleInfoList()) {
            configSync.addRole(CarpTripActivityPb.buildRoleConfig(roleInfo));
        }

        for (CarpTripEventInfo eventInfo : config.getEventInfoList()) {
            CarpTripProto.CarpTripEventTemp.Builder builder = CarpTripProto.CarpTripEventTemp.newBuilder();
            builder.setId(eventInfo.getId());
            builder.setType(eventInfo.getType());
            builder.setLittleType(eventInfo.getLittleType());
            builder.setExParam(eventInfo.getExParam());
            builder.setItemId(eventInfo.getItemId());
            configSync.addEvent(builder);
        }

//        for (CarpTripMultipleInfo multipleInfo : config.getMultipleInfoList()) {
//            CarpTripProto.CarpTripDrawMultipleTemp.Builder builder = CarpTripProto.CarpTripDrawMultipleTemp.newBuilder();
//            builder.setId(multipleInfo.getId());
//            builder.setValue(multipleInfo.getValue());
//            builder.setParam(multipleInfo.getParam());
//            builder.setWeight(multipleInfo.getWeight());
//            configSync.addMultiple(builder);
//        }
        for (CarpTripPicInfo tripPicInfo : config.getPicInfoList()) {
            CarpTripProto.CarpTripPicConfigTemp.Builder builder = CarpTripProto.CarpTripPicConfigTemp.newBuilder();
            builder.setId(tripPicInfo.getId());
            builder.setType(tripPicInfo.getType());
            builder.setCode(tripPicInfo.getCode());
            builder.setFishId(tripPicInfo.getFishId());
            builder.setFishLv(tripPicInfo.getFishLv());
            builder.setScale(tripPicInfo.getScale());
            configSync.addPic(builder);
        }

        for (CarpTripPvpTopInfo pvpTopInfo : config.getPvpTopList()) {
            CarpTripProto.CarpTripPvpTopConfigTemp.Builder builder = CarpTripProto.CarpTripPvpTopConfigTemp.newBuilder();
            builder.setCellNum(pvpTopInfo.getCellNum());
            builder.setAbility(pvpTopInfo.getAbility());
            builder.setPic(pvpTopInfo.getPic());
            builder.setReward(pvpTopInfo.getReward());
            configSync.addTop(builder);
        }

        for (CarpTripTemplateInfo templateInfo : config.getTemplateInfoList()) {
            CarpTripProto.CarpTripTemplateTemp.Builder builder = CarpTripProto.CarpTripTemplateTemp.newBuilder();
            builder.setId(templateInfo.getId());
            builder.addAllCodeA(StringUtils.stringToIntegerList(templateInfo.getCodeA(), "\\|"));
            configSync.addTemplate(builder);
        }

        for (CarpTripDragonUpInfo dragonUpInfo : config.getDragonUpInfoList()) {
            CarpTripProto.CarpTripDragonUpConfigTemp.Builder builder = CarpTripProto.CarpTripDragonUpConfigTemp.newBuilder();
            builder.setId(dragonUpInfo.getId());
            builder.setBorn(dragonUpInfo.getBorn());
            builder.setCondition(dragonUpInfo.getCondition());
            builder.setPic(dragonUpInfo.getPic());
            builder.setReward(dragonUpInfo.getReward());
            builder.setPicScale(dragonUpInfo.getPicScale());
            builder.setRolePic(dragonUpInfo.getRolePic());
            configSync.addDragonUp(builder);
        }

        for (CarpTripDrawUpInfo drawUpInfo : config.getDrawUpInfoList()) {
            CarpTripProto.CarpTripDrawUpConfigTemp.Builder builder = CarpTripProto.CarpTripDrawUpConfigTemp.newBuilder();
            builder.setId(drawUpInfo.getId());
            builder.setLevel(drawUpInfo.getLevel());
            builder.setExp(drawUpInfo.getExp());
            builder.setCost(drawUpInfo.getCost());
            builder.setShootNum(drawUpInfo.getShootNum());
            configSync.addDrawUp(builder);
        }

        for (CarpTripLevelInfo tripLevelInfo : config.getLevelInfoList()) {
            CarpTripProto.CarpTripLevelInfoTemp.Builder builder = CarpTripProto.CarpTripLevelInfoTemp.newBuilder();
            builder.setId(tripLevelInfo.getId());
            builder.setBossId(tripLevelInfo.getBossId());
            builder.setTemplateId(tripLevelInfo.getTemplateId());
            builder.setReward(tripLevelInfo.getReward());
            configSync.addLevelInfo(builder);
        }
    }

    public static CarpTripProto.CarpTripPVPReportTemp.Builder buildReportBuilder(CarpTripPvPReportData reportData) {
        CarpTripProto.CarpTripPVPReportTemp.Builder builder = CarpTripProto.CarpTripPVPReportTemp.newBuilder();
        builder.setEnemyId(reportData.getEnemyUserId());
        builder.setAbility(reportData.getEnemyAbility());
        builder.setMyAbility(reportData.getAbility());
        builder.setBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(reportData.getEnemyUserBaseInfo()));
        builder.setTime(reportData.getTime());
        builder.setNewCellId(reportData.getNewCellNum());
        builder.setOldCellId(reportData.getOldCellNum());
        return builder;
    }
}
